package be.gnx.fogo.application.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import be.gnx.fogo.application.util.HibernateUtil;

public class Category {

  private Long id;

  private String family;

  private String name;

  public static void initialise() {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    Transaction transaction = session.getTransaction();
    transaction.begin();
    Query query = session.createQuery("from Category");
    query.setReadOnly(true);
    List<Category> categories = query.list();
    HashMap<String, HashMap<String, Category>> categoriesHashMap = new HashMap<String, HashMap<String,Category>>();
    for (Category category : categories) {
      HashMap<String, Category> hashMap = categoriesHashMap.get(category.family);
      if (hashMap == null) {
        hashMap = new HashMap<String, Category>();
        categoriesHashMap.put(category.family, hashMap);
      }
      hashMap.put(category.name, category);
    }

    final List<Category> initialCategories = new ArrayList<Category>(); {
      initialCategories.add(new Category("COMMUNICATION", "Primary Personal E-Mail"));
      initialCategories.add(new Category("COMMUNICATION", "Auxiliary Personal E-Mail"));
      initialCategories.add(new Category("COMMUNICATION", "Primary Business E-Mail")); 
      initialCategories.add(new Category("COMMUNICATION", "Auxiliary Business E-Mail"));
      initialCategories.add(new Category("COMMUNICATION", "Primary Personal Address"));
      initialCategories.add(new Category("COMMUNICATION", "Auxiliary Personal Address"));
      initialCategories.add(new Category("COMMUNICATION", "Primary Business Address"));
      initialCategories.add(new Category("COMMUNICATION", "Auxiliary Business Address"));
      initialCategories.add(new Category("COMMUNICATION", "Primary Personal Phone Number"));
      initialCategories.add(new Category("COMMUNICATION", "Primary Personal Fax Number"));
      initialCategories.add(new Category("COMMUNICATION", "Primary Personal Mobile Number"));
      initialCategories.add(new Category("COMMUNICATION", "Auxiliary Personal Phone Number"));
      initialCategories.add(new Category("COMMUNICATION", "Auxiliary Personal Fax Number"));
      initialCategories.add(new Category("COMMUNICATION", "Auxiliary Personal Mobile Number"));
      initialCategories.add(new Category("COMMUNICATION", "Primary Business Phone Number"));
      initialCategories.add(new Category("COMMUNICATION", "Primary Business Fax Number"));
      initialCategories.add(new Category("COMMUNICATION", "Primary Business Mobile Number"));
      initialCategories.add(new Category("COMMUNICATION", "Auxiliary Business Phone Number"));
      initialCategories.add(new Category("COMMUNICATION", "Auxiliary Business Fax Number"));
      initialCategories.add(new Category("COMMUNICATION", "Auxiliary Business Mobile Number"));
      initialCategories.add(new Category("PERSON", "Genodigde mis huwelijk Leen en Wim"));
      initialCategories.add(new Category("PERSON", "Genodigde receptie huwelijk Leen en Wim"));
      initialCategories.add(new Category("PERSON", "Genodigde diner huwelijk Leen en Wim"));
      initialCategories.add(new Category("PERSON", "Uitnodiging verstuurd huwelijk Leen en Wim"));
      initialCategories.add(new Category("PERSON", "Komt naar huwelijk Leen en Wim"));
      initialCategories.add(new Category("PERSON", "Komt niet naar huwelijk Leen en Wim"));
      initialCategories.add(new Category("PERSON", "Genodigde surprise party 30 jaar Els Van Hoeck"));
      initialCategories.add(new Category("PERSON", "Krijgt een kerstkaart van ons"));
      initialCategories.add(new Category("PERSON", "Krijgt suikerbonen van ons"));
      initialCategories.add(new Category("PERSON", "Krijgt geboortekaart jef van ons"));
      initialCategories.add(new Category("PERSON", "Krijgt geboortekaart emma van ons"));
      initialCategories.add(new Category("PERSON", "Kookles Els"));
      initialCategories.add(new Category("PERSON", "Muzikant trouw"));
      initialCategories.add(new Category("PERSON", "Klanten Splendor"));
      initialCategories.add(new Category("PERSONEXTRAINFO", "Cadeau Jef"));
      initialCategories.add(new Category("PERSONEXTRAINFO", "Cadeau trouw"));
      initialCategories.add(new Category("PERSONEXTRAINFO", "Medisch"));
      initialCategories.add(new Category("PERSONEXTRAINFO", "Organisatie"));
    }
    for (Category category : initialCategories) {
      if (!categoriesHashMap.containsKey(category.family)) {
        session.persist(category);
        session.save(category);
      } else if (!categoriesHashMap.get(category.family).containsKey(category.name)) {
        session.persist(category);
        session.save(category);
        }

    }
    transaction.commit();
  }

  public static Category get(String family, String name) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    Query query = session.createQuery("from Category where family = ? and name = ?");
    query.setReadOnly(true);
    query.setString(0, family);
    query.setString(1, name);
    List<Category> categories = query.list();
    if (categories.size() > 0) {
      return categories.get(0);
    } else {
      return null;
    }
  }

  public Category() {
    
  }

  private Category(String family, String name) {
    this.family = family;
    this.name = name;
  }

  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public String getFamily() {
    return family;
  }

  public void setFamily(String family) {
    this.family = family;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

}
